﻿

function systole() {
    if (!$(".history").length) {
        return;
    }
    var $warpEle = $(".history-date"),
		$targetA = $warpEle.find("h2 a,ul li dl dt a"),
		parentH,
		eleTop = [];

    parentH = $warpEle.parent().height() + 80;


    setTimeout(function () {

        $warpEle.find("ul").children(":not('h2:first')").each(function (idx) {
            eleTop.push($(this).position().top);
            $(this).css({ "margin-top": -eleTop[idx] }).children().hide();
        }).animate({ "margin-top": 0 }, 1600).children().fadeIn();


        $warpEle.find("ul").children(":not('h2:first')").addClass("bounceInDown").css({ "-webkit-animation-duration": "2s", "-webkit-animation-delay": "0", "-webkit-animation-timing-function": "ease", "-webkit-animation-fill-mode": "both" }).end().children("h2").css({ "position": "relative" });

    }, 100);

    $targetA.click(function () {
        $(this).parent().css({ "position": "relative" });
        $(this).parent().siblings().slideToggle();
        $warpEle.parent().removeAttr("style");
        return false;
    });
};




var bufferCount = 1;
var isLoaded = true;

var ajaxGetMiniBlog = function (ishashchange, isBufferRetry) {
    var url;
    if (!window.location.hash || window.location.hash.indexOf('#!') < 0) {
        url = window.location.href;

    }
    else {
        url = window.location.href.replace(App.config.Domain, "").split('!')[1];

    }


    var loadMB = $("#loadMB");
    var loadError = $("#loadError");
    var loadNoResult = $("#loadNoresult");

    var loadMB1 = $("#loadMB1");
    var loadError1 = $("#loadError1");
    var loadNoResult1 = $("#loadNoresult1");

    if (ishashchange) {
        bufferCount = 1;
        loadMB.show();
        loadError.hide();
        loadNoResult.hide();
    }
    else {
        if (!isBufferRetry)
            bufferCount++;
        loadMB1.show();
        loadError1.hide();
        loadNoResult1.hide();
    }

    url = App.AddQueryToUrl(url.replace(/#/g, ""), "Buffer", bufferCount);

    isLoaded = false;
    $.ajax({
        dataType: "json",
        url: url,
        cache: false,
        type: "get",
        success: function (o) {
            console.log(o.Code);
            if (o.Code == "A00005") {
                if (ishashchange) {

                    loadMB.hide();
                    $(".MIB_feed").remove();
                    $("#page").remove();
                    $(o.Data).insertAfter(loadNoResult);

                    loadMB1.remove();
                    loadNoResult1.remove();
                    loadError1.remove();
                }
                else {

                    // $("#aMoreDate").remove();
                    loadMB.remove();
                    $(o.Data).insertAfter(loadNoResult1);

                    loadMB1.remove();
                    loadNoResult1.remove();
                    loadError1.remove();
                }
            }
            else {
                if (ishashchange) {

                    loadMB.hide();
                    loadNoResult.show();
                }
                else {

                    loadMB1.hide();
                    loadNoResult1.show();
                }
            }
            isLoaded = true;
        },
        error: function () {

            if (ishashchange) {

                loadMB.hide();
                loadMB1.hide();
                loadNoResult.hide();
                loadError.show();
            }
            else {

                loadMB.hide();
                loadError.hide();

                loadMB1.hide();
                loadNoResult1.hide();
                //loadError1.show();
            }
            isLoaded = true;
        }
    });
};

function moreDateClick(obj) {
    $(obj).remove();
    ajaxGetMiniBlog();
}



$(document).ready(function () {

    App.bindGoTop(curBorder);
    ajaxGetMiniBlog(true);
    systole();

    $(window).scroll(function () {
        if ($("#loadMB1")[0]) {
            if ($(this).scrollTop() + $(this).height() > $("#loadMB1").offset().top) {
                if (bufferCount < 3 && isLoaded) {
                    ajaxGetMiniBlog();
                }
            }
        }
        if (bufferCount < 3 && $("#aMoreDate")[0]) {
            $("#aMoreDate").remove();
        }
        if (bufferCount == 3 && $("#aMoreDate")[0]) {
            $("#loadMB").hide();
            $("#loadError").hide();
            $("#loadNoresult").hide();
            $("#loadMB1").hide();
            $("#loadError1").hide();
            $("#loadNoresult1").hide();

        }
        if (bufferCount > 3 && !$("#aMoreDate")[0]) {
            $("#loadMB").remove();
            $("#loadError").remove();
            $("#loadNoresult").remove();
            $("#loadMB1").remove();
            $("#loadError1").remove();
            $("#loadNoresult1").remove();

        }
    });
});
 